package ks.client.gui.admin.user.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JTable;
import ks.client.controllers.admincommands.UserCreateCommand;
import ks.client.gui.ErrorMessageGUI;
import ks.client.lobby.Lobby;
import ks.managers.user.User;

/**
 * This controller will send a command to create a bunch of user accounts as defined
 * in the table whenever the "Batch Create Users" button is clicked.An error message is returned
 * if the table is empty.
 * 
 * @author Jennifer Hunt
 *
 */
public class BatchCreateUsersClickController implements ActionListener{
	
	Lobby lobby;
	JTable table;
	protected Hashtable<String, User> users = new Hashtable<String, User>();
	
	/**
	 * Constructor for BatchCreateUsersClickController
	 * @param lobby
	 */
	public BatchCreateUsersClickController(Lobby lobby, JTable table) {
		this.lobby = lobby;
		this.table = table;		
	}
	
	/**
	 * 
	 * When the "Batch Create Users" button is clicked, if the table is not empty
	 * then a user-create command is sent to the server.
	 * 
	 */
	public void actionPerformed(ActionEvent e){
		if(table.getRowCount()==0){
			JFrame pma = new ErrorMessageGUI("Please add a user to the able in order to create.");
			pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			pma.setTitle("Error");
			pma.setVisible(true);
		}
		else{
			int index = 0;
			
			while(index < table.getRowCount()){
				User u = new User(table.getValueAt(index, 0).toString(), table.getValueAt(index, 1).toString());
				users.put(u.name, u);
				
				index++;
			}
			new UserCreateCommand(lobby).send(users.values());
		}
	}
}
